package main.java.tmi.actualStockList.web;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.util.StringUtil;
import main.java.tmi.actualStockList.service.ActualStockListService;
import main.java.tmi.common.service.UserVO;
import main.java.tmi.common.util.AjaxPageSplit;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


/**
 *  Controller
 *
 * @author min™
 * @since 2012. 05. 08.
 */
@Controller
public class ActualStockListController {
	
    @Resource(name = "actualStockListServiceImpl")
    private ActualStockListService actualStockListService;
    
    /**
     * The Constructor
     */
    public ActualStockListController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/actualStockList")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============actualStockList start=========================");
        System.out.println("====================================================");
        UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
		model.addAttribute("custCodeUser",userInfo.getCustCode());
		model.addAttribute("roleId",userInfo.getRoleId());
        return "/tmi/actualStockList/actualStockList";
    }   
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getActualStockListView")
    public void getActualStockListView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getActualStockListView start");
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int actualStockListCount = actualStockListService.getActualStockListViewCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(actualStockListCount, pagingTmp, listScale, 5, "");
		System.out.println("===========pageSplit============> "+pageSplit);
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("actualStockListCount",actualStockListCount);
		model.addAttribute("resultList",actualStockListService.getActualStockListView(param));
    }
    
	@RequestMapping(value = "/actualStockListExcel")
	public String viewExcel(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {		
		Map<String,String> param = StringUtil.getParameterMap(request);
		param.put("paging", "0");
		param.put("listScale", "100000");
		
        int actualStockListCount = actualStockListService.getActualStockListViewCount(param);
         
        String type1 = "";
        String type2 = "";
        String type3 = "";
        
        if(param.get("all").toString().equals("1")){
        	type1 = type1 + "ALL ,";
        }
        
        if(param.get("onBoard").toString().equals("1")){
        	type1 = type1 + "ON BOARD ,";
        }
        
        if(param.get("shipmentStatus").toString().equals("1")){
        	type1 = type1 + "SHIPMENT STATUS ,";
        }
        
        if(param.get("clearance").toString().equals("1")){
        	type1 = type1 + "CLEARANCE ,";
        }
        
        if(param.get("stockToWh").toString().equals("1")){
        	type1 = type1 + "STOCK TO W/H ,";
        }
        
        if(param.get("stockIn").toString().equals("1")){
        	type1 = type1 + "STOCK IN ,";
        }
        
        if(param.get("stockOut").toString().equals("1")){
        	type1 = type1 + "STOCK OUT ,";
        }
        
        if(param.get("delivery").toString().equals("1")){
        	type1 = type1 + "DELIVERY ,";
        }
        
        if(param.get("returnStock").toString().equals("1")){
        	type1 = type1 + "RETURN";
        }
        
        if(param.get("adjust").toString().equals("1")){
        	type1 = type1 + "ADJUST";
        }
        
        if(param.get("qurantine").toString().equals("1")){
        	type2 = type2 + "QURANTINE";
        }
        
        if(param.get("stockToBeDisposal").toString().equals("1")){
        	type2 = type2 + "STOCK TO BE DISPOSAL";
        }
        
        if(param.get("disposal").toString().equals("1")){
        	type2 = type2 + "DISPOSAL";
        }
        
        if(param.get("loss").toString().equals("1")){
        	type3 = type3 + "LOSS";
        }
        
        model.addAttribute("mawb",param.get("mawb"));
        model.addAttribute("hawb",param.get("hawb"));
        model.addAttribute("batchNo",param.get("batchNo"));
        model.addAttribute("sStartDate",param.get("sStartDate"));
        model.addAttribute("sEndDate",param.get("sEndDate"));
        
        model.addAttribute("goodsName",param.get("goodsName"));
        model.addAttribute("sShipperName",param.get("sShipperName"));
        model.addAttribute("sMessrsName",param.get("sMessrsName"));
        
        model.addAttribute("type1",type1);
        model.addAttribute("type2",type2);
        model.addAttribute("type3",type3);
                
        
		model.addAttribute("actualStockListCount",actualStockListCount);
		model.addAttribute("resultList",actualStockListService.getActualStockListView(param));
		
		
	    
		return "/tmi/actualStockList/actualStockListExcel";
	}
}
